package cn.edu.scau.cmi.yangmupei.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.edu.scau.cmi.yangmupei.domain.Teacher;
import cn.edu.scau.cmi.yangmupei.util.ScauCmiHibernateSessionFactoryUtil;

public class TeacherDAO {
	private static final String NAME = "name";
	private Session session;
	
	public void save(Teacher transienInstance) {
		getSession();
		Transaction transaction = session.beginTransaction();
		session.save(transienInstance);
		transaction.commit();
		System.out.println("添加成功");
	}
	
	public void delete(Teacher persistentInstance) {
		try {
			getSession();
			Transaction transaction = session.beginTransaction();
			session.delete(persistentInstance);
			transaction.commit();
			System.out.println("删除成功");
		} catch (RuntimeException re) {
			System.out.println("删除失败");
			throw re;
		}
	}
	
	public Teacher findById( int id) {
		
		try {
			Teacher instance = (Teacher) getSession().get("cn.edu.scau.cmi.yangmupei.domain.Teacher", id);
			return instance;
		} catch (RuntimeException re) {
			
			throw re;
		}
	}
	public void merge(Teacher detachedInstance) {
		try {
			getSession();
			Transaction transaction = session.beginTransaction();
			session.merge(detachedInstance);
			transaction.commit();
			System.out.println("修改成功");
//			return result;
		} catch (RuntimeException re) {
			throw re;
		}
	}


	private Session getSession() {
		session=ScauCmiHibernateSessionFactoryUtil.getSession();
		return session;
	}
}
